<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\BaseController;
use App\Models\Comment;
use App\Models\Material;
use App\Models\Work;
use Dingo\Api\Http\Request;

class CommentController extends BaseController
{

    public $model = Comment::class;


    public function index(Request $request) {
        $this->validate($request, [
            'parent_id' => 'filled|int'
        ]);
        $message = $request->input('message');

        $search = ['join_nick_name' => null, 'parent_id' => 0, 'type' => null];

        $parent_id = $request->input('parent_id', 0);
        $input = $request->input();
        $list = Comment::withCertain('refer_user', ['nickname', 'avatar_url'])->withCertain('user', ['nickname', 'avatar_url'])
            ->apply($search, $input)
            ->when($message, function ($q) use($message) {
                $q->where('message', 'like', '%' . $message . '%');
            })
            ->orderBy("id", "desc")
            ->paginate(per_page());

        foreach ($list as &$item) {
            if ($item->type == Comment::TYPE_WORK) {
                $item->title = Work::find($item->entity_id)->title ?? '';
            } else if($item->type == Comment::TYPE_MATERIAL) {
                $item->title = Material::find($item->entity_id)->title ?? '';
            } else {
                $item->tilte = '';
            }
        }

        $info = null;
        if ($parent_id) {
            $info = Comment::withCertain('user', ['nickname', 'avatar_url'])->find($parent_id);
        }
        return $this->paginate($list, $info);
    }


    /**
     * 删除
     * @param Request $request
     * @return mixed
     * @throws \Illuminate\Validation\ValidationException
     * @throws \Throwable
     */
    public function destroy(Request $request)
    {
        $this->validate($request, [
            'comment_id' => 'required',
        ]);
        $id = \request('comment_id');
        $comment = Comment::find($id);
        if (!$comment)
            return $this->failed("评论不存在");


        $parent_id = $comment->parent_id;
        return \DB::transaction(function () use ($parent_id, $comment) {
            if ($parent_id) {
                $total = $comment->reply_count;
                $comment->delete();
                Comment::where('id', $parent_id)->increment('reply_count', -1);
            } else {
                $total = $comment->reply_count + 1;
                Comment::where('parent_id', $comment->id)->delete();
                $comment->delete();
            }

            $entity_model = null;
            if ($comment->entity_id == Comment::TYPE_WORK) {
                $entity_model = new Work();
            }  else if($comment->entity_id == Comment::TYPE_MATERIAL) {
                $entity_model = new Material();
            }

            $entity_model && $entity_model->where('id', $comment->entity_id)->increment('comment_num', -$total);
            return $this->success($comment->id);
        });
    }


}

